package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventGkcs;
import com.ruoyi.event.domain.bo.EventGkcsBo;
import com.ruoyi.event.domain.vo.EventGkcsVo;
import com.ruoyi.event.mapper.EventGkcsMapper;
import com.ruoyi.event.service.IEventGkcsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 管控措施Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventGkcsServiceImpl implements IEventGkcsService {

    private final EventGkcsMapper baseMapper;

    /**
     * 查询管控措施
     */
    @Override
    public EventGkcsVo queryById(Long gkcsId) {
        return baseMapper.selectVoById(gkcsId);
    }

    /**
     * 查询管控措施列表
     */
    @Override
    public TableDataInfo<EventGkcsVo> queryPageList(EventGkcsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventGkcs> lqw = buildQueryWrapper(bo);
        Page<EventGkcsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询管控措施列表
     */
    @Override
    public List<EventGkcsVo> queryList(EventGkcsBo bo) {
        LambdaQueryWrapper<EventGkcs> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventGkcs> buildQueryWrapper(EventGkcsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventGkcs> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventGkcs::getUserId, bo.getUserId());
        lqw.eq(bo.getSj() != null, EventGkcs::getSj, bo.getSj());
        lqw.eq(StringUtils.isNotBlank(bo.getLxqf()), EventGkcs::getLxqf, bo.getLxqf());
        lqw.eq(StringUtils.isNotBlank(bo.getFj()), EventGkcs::getFj, bo.getFj());
        lqw.eq(StringUtils.isNotBlank(bo.getCsnr()), EventGkcs::getCsnr, bo.getCsnr());
        return lqw;
    }

    /**
     * 新增管控措施
     */
    @Override
    public Boolean insertByBo(EventGkcsBo bo) {
        EventGkcs add = BeanUtil.toBean(bo, EventGkcs.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGkcsId(add.getGkcsId());
        }
        return flag;
    }

    /**
     * 修改管控措施
     */
    @Override
    public Boolean updateByBo(EventGkcsBo bo) {
        EventGkcs update = BeanUtil.toBean(bo, EventGkcs.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventGkcs entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除管控措施
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
